package com.example.demo.listener;

import com.example.demo.entity.CSDNArticleEntity;
import com.example.demo.mapper.CSDNMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author haohulala
 * @version 1.0
 * @description: 消费文章信息
 * @date 2023/4/15 21:43
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "TOPIC_CSDN", selectorExpression = "ARTICLE", consumerGroup = "CSDN_ARTICLE")
public class ArticleListener implements RocketMQListener<CSDNArticleEntity> {
    @Autowired
    private CSDNMapper csdnMapper;

    @Override
    public void onMessage(CSDNArticleEntity csdnArticleEntity) {
        if(csdnMapper.checkArticle(csdnArticleEntity)==0) {
            csdnMapper.insertOneArticle(csdnArticleEntity);
        }
        else {
            csdnMapper.updateOneArticle(csdnArticleEntity);
        }
        log.info("消费文章信息：{}", csdnArticleEntity);
    }
}
